package com.hr.mes.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hr.mes.dto.Page;
import com.hr.mes.entity.Role;
import com.hr.mes.entity.User;
import com.hr.mes.entity.UserRole;
import com.hr.mes.mapper.RoleMapper;
import com.hr.mes.mapper.UserMapper;
import com.hr.mes.mapper.UserRoleMapper;
import com.hr.mes.service.UserService;
import com.hr.mes.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void addUser(@RequestBody User user) {
        userMapper.addUser(user);
        List<Role> roleList = user.getRoleList();
        if(!CollectionUtils.isEmpty(roleList)){
            for (Role role : roleList){
                userRoleMapper.addUserRole(new UserRole(user.getUserId(), role.getRoleId()));
            }
        }
    }

    @Override
    public void deleteUser(@RequestParam Integer userId) {
        userMapper.deleteUser(userId);
        userRoleMapper.deleteUserRoleByUserId(userId);
    }

    @CacheEvict(value = "shiro:auth", key = "#userId")
    @Override
    public void updateUser(@RequestBody User user) {
        userMapper.updateUser(user);
        List<Role> roleList = user.getRoleList();
        if(!CollectionUtils.isEmpty(roleList)){
            userRoleMapper.deleteUserRoleByUserId(user.getUserId());
            for (Role role : roleList){
                userRoleMapper.addUserRole(new UserRole(user.getUserId(), role.getRoleId()));
            }
        }
    }

    @Override
    public User getUser(@RequestParam Integer userId) {
        User user = userMapper.getUser(userId);
        List<UserRole> userRoleList = userRoleMapper.getUserRoleList(userId);
        if(!CollectionUtils.isEmpty(userRoleList)){
            ArrayList<Role> roleList = new ArrayList<>();
            for (UserRole userRole : userRoleList){
                Role role = roleMapper.getRole(userRole.getRoleId());
                roleList.add(role);
            }
            user.setRoleList(roleList);
        }
        return user;
    }

    @Override
    public Page<User> getUserPage(@RequestParam Integer pageNo, @RequestParam Integer pageSize) {
        List<User> userList = userMapper.getUserList();
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        Page<User> page = PageUtil.ConvertPageInfoToPage(pageInfo);
        PageHelper.clearPage();
        return page;
    }

    @Override
    public List<User> getUserList() {
        List<User> userList = userMapper.getUserList();
        return userList;
    }

    @Override
    public User getUserByEmployeeNumber(String employeeNumber) {
        User user = userMapper.getUserByEmployeeNumber(employeeNumber);
        user = getUser(user.getUserId());
        return user;
    }

}
